package com.app.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.app.bean.Kcxx;
import com.app.bean.KcxxExample;
import com.app.bean.KcxxExample.Criteria;
import com.app.bean.Slxx;
import com.app.mapper.KcxxMapper;
import com.app.mapper.SlxxMapper;
import com.pro.util.StringUtil;
import com.pro.util.StringUtils;

import net.sf.json.JSONArray;

@Controller
@RequestMapping("/kcxx")
public class KcxxController extends BaseController{
	
	@Autowired
	private KcxxMapper kcxxMapper;
	
	@Autowired
	private SlxxMapper slxxMapper;
	
	
	@RequestMapping("/getWp")
	public void getWp(HttpServletRequest request,HttpServletResponse response,Kcxx bean,String wpCount2){
		init(request);
		
		try{
			
			//物品领用数量超限检查
			Integer wpCount = bean.getWpCount();
			Integer wpCountForGet = Integer.parseInt(wpCount2);
			
			if(wpCountForGet > wpCount){
				result.setCode(-1);
				result.setErrMsg("业务出现异常：库存余量不足！");
				endProcess(response, result);
				return;
			}
			
			//申领单据组装数据
			Slxx slxx = new Slxx();
			slxx.setSlTime(StringUtils.getCurrentDateForMysql24()); //申领时间
			slxx.setStatus("0"); //待审核状态
			slxx.setUserid(loginUser.getUserId());//当前领用人
			slxx.setUsername(loginUser.getUserDescription());
			slxx.setWpCount(wpCountForGet);
			slxx.setWpid(bean.getId());
			slxx.setWpmc(bean.getWpmc());
			slxx.setWpxh(bean.getWpxh());
			
			//单据入库
			
			slxxMapper.insert(slxx);
			
			
			
			
		}catch(Exception e){
			e.printStackTrace();
			result.setCode(-2);
			result.setErrMsg("系统错误" + e.getMessage());
		}
		
		
		endProcess(response, result);
		
	}
	
	@RequestMapping("/remove")
	public void remove(HttpServletRequest request,HttpServletResponse response,Kcxx bean){
		init(request);
		
		try{
			
			kcxxMapper.deleteByPrimaryKey(bean.getId());
			
		}catch(Exception e){
			e.printStackTrace();
			result.setCode(-2);
			result.setErrMsg("系统错误" + e.getMessage());
		}
		
		
		endProcess(response, result);
		
	}
	
	@RequestMapping("/list")
	public void list(HttpServletRequest request,HttpServletResponse response,String s_wpmc){
		init(request);
		
		try{
			KcxxExample example = new KcxxExample();
			Criteria criteria = example.createCriteria();
			
			if(StringUtil.isNotEmpty(s_wpmc)){
				criteria.andWpmcLike("%"+s_wpmc+"%");
			}
			
			List<Kcxx> list = kcxxMapper.selectByExample(example);
			result.setRows(JSONArray.fromObject(list));
			
		}catch(Exception e){
			e.printStackTrace();
			result.setCode(-2);
			result.setErrMsg("系统错误" + e.getMessage());
		}
		
		
		endProcess(response, result);
		
	}
	
	@RequestMapping("/save")
	public void save(HttpServletRequest request,HttpServletResponse response,Kcxx bean){
		init(request);
		
		try{
			
			if(bean.getId() == null){
				kcxxMapper.insert(bean);
			}else{
				kcxxMapper.updateByPrimaryKeySelective(bean);
			}
			
			
		}catch(Exception e){
			e.printStackTrace();
			result.setCode(-2);
			result.setErrMsg("系统错误" + e.getMessage());
		}
		
		
		endProcess(response, result);
		
	}
	

}
